﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;
using System.Text;
using System.Drawing;

namespace Util
{
    public class CodeGenerate
    {

        private static string codeGeneratePath;
        static private object objectLock = new object();
        public static string CodeGeneratePath
        {
            get
            {
                if (CodeGeneratePath == null || CodeGeneratePath.Length == 0 || CodeGeneratePath.Trim() == "")
                {
                    lock (objectLock)
                    {
                        //string sqlc = System.Web.HttpContext.Current.Server.MapPath("..") + System.Configuration.ConfigurationManager.AppSettings["oleconnectionString"];
                        string path = System.Configuration.ConfigurationManager.AppSettings["CodeGeneratePath"];

                        if (path != null && path.Length > 0)
                            codeGeneratePath = path;

                    }
                }
                return CodeGenerate.codeGeneratePath;
            }
        }
        public CodeGenerate()
        { }

        public static Result BuildTablesBLL(string txtnamespace)
        {
            Result rs = new Result();
            try
            {             
                DataView dv = OleDBCommon.OleDBObject.GetTableList().DefaultView;
                for (int i = 0; i < dv.Count; i++)
                {
                    string name = dv[i][2].ToString();
                    Util.CodeGenerate.BuildDBAccess("Id", name, txtnamespace);                
                }
                rs.IsSuccess = true;
            }
            catch (Exception ex)
            {
                rs.IsSuccess = false;
                rs.Msg = ex.Message;
            }
            return rs;
        }
        public static Result BuildTablesDBAccess(string txtnamespace)
        {
            Result rs = new Result();
            try
            {
                DataView dv = OleDBCommon.OleDBObject.GetTableList().DefaultView;
                for (int i = 0; i < dv.Count; i++)
                {
                    string name = dv[i][2].ToString();
                    Util.CodeGenerate.BuildBLL(txtnamespace, name);
                }
                rs.IsSuccess = true;
            }
            catch (Exception ex)
            {
                rs.IsSuccess = false;
                rs.Msg = ex.Message;
            }
            return rs;
        }
        public static Result BuildTablesModel(string txtnamespace)
        {
            Result rs = new Result();
            try
            {
                DataView dv = OleDBCommon.OleDBObject.GetTableList().DefaultView;

                for (int i = 0; i < dv.Count; i++)
                {
                    string name = dv[i][2].ToString();
                    CodeGenerate.BuildModel(txtnamespace, name);
                }
                rs.IsSuccess = true;
            }
            catch (Exception ex)
            {
                rs.IsSuccess = false;
                rs.Msg = ex.Message;
            }
            return rs;
        }
        public static string BuildBLL(string strNamespace, string tbname)
        {
            string strMsg = "";
            if (strNamespace != null || strNamespace.Length != 0 || strNamespace != "")
                strNamespace = "." + strNamespace;
            else
                strNamespace = "";
            StringBuilder strB = new StringBuilder();  
            //引用 说明
            AppendSpaceLine(ref strB, 0, "/***********************");
            AppendSpaceLine(ref strB, 0, "*");
            AppendSpaceLine(ref strB, 0, "* Author:彭承龙");
            AppendSpaceLine(ref strB, 0, "* 介绍：Access数据库【业务逻辑层自动生成】");
            AppendSpaceLine(ref strB, 0, "* 生成时间:"+DateTime.Now.ToString("yyyy年MM月dd日 HH:mm"));
            AppendSpaceLine(ref strB, 0, "*");
            AppendSpaceLine(ref strB, 0, "************************/");
            AppendSpaceLine(ref strB, 0, "using System;");
            AppendSpaceLine(ref strB, 0, "using System.Collections.Generic;");
            AppendSpaceLine(ref strB, 0, "using System.Linq;");
            AppendSpaceLine(ref strB, 0, "using System.Text;");
            AppendSpaceLine(ref strB, 0, "using System.Data;");
            AppendSpaceLine(ref strB, 0, "using System.Data.OleDb;");
            AppendSpaceLine(ref strB, 0, "using Util;");
            //命名空间 类名开始
            AppendSpaceLine(ref strB, 0,"namespace Bussiness"+strNamespace);
            AppendSpaceLine(ref strB, 0,"{");
            AppendSpaceLine(ref strB, 1,"/// <summary>");
            AppendSpaceLine(ref strB, 1,"/// 业务逻辑类"+tbname+"B");
            AppendSpaceLine(ref strB, 1,"/// </summary>");
            AppendSpaceLine(ref strB, 2,"public class UserInfoB");
            AppendSpaceLine(ref strB, 2,"{");
            #region  //方法体 Add
            AppendSpaceLine(ref strB, 3,"/// <summary>");
            AppendSpaceLine(ref strB, 3,"/// 添加一个"+tbname+"记录");
            AppendSpaceLine(ref strB, 3,"/// </summary>");
            AppendSpaceLine(ref strB, 3,"/// <Decription>此代码由CodeGenerate自动生成，作者：彭承龙</Decription>");  
            AppendSpaceLine(ref strB, 3,"/// <param name=\"tb\"></param>");
            AppendSpaceLine(ref strB, 3,"/// <returns></returns>");
            AppendSpaceLine(ref strB, 3,"public static Result Add(Model"+strNamespace+"."+tbname+" tb)");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3,"OleDbConnection sqlCon = new OleDbConnection(Util.OleDbHelper.ConnectionString);");
            AppendSpaceLine(ref strB, 3,"sqlCon.Open();");        
            AppendSpaceLine(ref strB, 3,"Result rs = new Result();");        
            AppendSpaceLine(ref strB, 3,"OleDbTransaction SqlTran = sqlCon.BeginTransaction();");        
            AppendSpaceLine(ref strB, 3,"try");        
            AppendSpaceLine(ref strB, 3,"{");        
            AppendSpaceLine(ref strB, 3,"rs =DBAccess"+strNamespace+"."+tbname+"D.Add(SqlTran, tb);");        
            AppendSpaceLine(ref strB, 3,"if (rs.IsSuccess)");        
            AppendSpaceLine(ref strB, 3,"{");        
            AppendSpaceLine(ref strB, 3,"SqlTran.Commit();");        
            AppendSpaceLine(ref strB, 3,"}"); 
            AppendSpaceLine(ref strB, 3,"else"); 
            AppendSpaceLine(ref strB, 3,"{"); 
            AppendSpaceLine(ref strB, 3,"SqlTran.Rollback();"); 
            AppendSpaceLine(ref strB, 3,"}"); 
            AppendSpaceLine(ref strB, 3,"}"); 
            AppendSpaceLine(ref strB, 3,"catch (Exception ex)"); 
            AppendSpaceLine(ref strB, 3,"{"); 
            AppendSpaceLine(ref strB, 3,"SqlTran.Rollback();"); 
            AppendSpaceLine(ref strB, 3,"rs.Msg = ex.Message;"); 
            AppendSpaceLine(ref strB, 3,"throw ex;"); 
            AppendSpaceLine(ref strB, 3,"}"); 
            AppendSpaceLine(ref strB, 3,"finally"); 
            AppendSpaceLine(ref strB, 3,"{"); 
            AppendSpaceLine(ref strB, 3,"if (sqlCon.State == System.Data.ConnectionState.Open)"); 
            AppendSpaceLine(ref strB, 3,"{"); 
            AppendSpaceLine(ref strB, 3,"sqlCon.Close();"); 
            AppendSpaceLine(ref strB, 3,"}"); 
            AppendSpaceLine(ref strB, 3,"SqlTran.Dispose();"); 
            AppendSpaceLine(ref strB, 3,"sqlCon.Dispose();");
            AppendSpaceLine(ref strB, 3,"}");
            AppendSpaceLine(ref strB, 3,"return rs;");
            AppendSpaceLine(ref strB, 3, "}");
            //--------------
            #endregion 
            #region //方法体 Update

            AppendSpaceLine(ref strB, 3,"/// <summary>");
            AppendSpaceLine(ref strB, 3,"/// 更新一个UserInfo记录");
            AppendSpaceLine(ref strB, 3,"/// </summary>");
            AppendSpaceLine(ref strB, 3,"/// <Decription>此代码由CodeGenerate自动生成，作者：彭承龙</Decription>");
            AppendSpaceLine(ref strB, 3,"/// <param name=\""+tbname.Substring(0,1).ToLower()+tbname.Substring(1)+"\"></param>");
            AppendSpaceLine(ref strB, 3,"/// <returns></returns>");
            AppendSpaceLine(ref strB, 3, "public static Result Update(Model" + strNamespace + "." + tbname + " " + tbname.Substring(0, 1).ToLower() + tbname.Substring(1) + ")");
            AppendSpaceLine(ref strB, 3, "{");
            AppendSpaceLine(ref strB, 3," OleDbConnection sqlCon = new OleDbConnection(Util.OleDbHelper.ConnectionString);");
            AppendSpaceLine(ref strB, 3,"sqlCon.Open();");
            AppendSpaceLine(ref strB, 3,"Result rs = new Result();");
            AppendSpaceLine(ref strB, 3," OleDbTransaction SqlTran = sqlCon.BeginTransaction();");           
            AppendSpaceLine(ref strB, 3,"try");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3, "rs = DBAccess" + strNamespace + "." + tbname + "D.Update(SqlTran, " + tbname.Substring(0, 1).ToLower() + tbname.Substring(1) + ");");

            AppendSpaceLine(ref strB, 3,"if (rs.IsSuccess)");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3,"  SqlTran.Commit();");
            AppendSpaceLine(ref strB, 3," }");
            AppendSpaceLine(ref strB, 3,"else");
            AppendSpaceLine(ref strB, 3," {");
            AppendSpaceLine(ref strB, 3," SqlTran.Rollback();");
            AppendSpaceLine(ref strB, 3,"}");
            AppendSpaceLine(ref strB, 3,"}");
            AppendSpaceLine(ref strB, 3,"catch (Exception ex)");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3," SqlTran.Rollback();");

            AppendSpaceLine(ref strB, 3," rs.Msg = ex.Message;");

            AppendSpaceLine(ref strB, 3,"throw ex;");
            AppendSpaceLine(ref strB, 3,"}");
            AppendSpaceLine(ref strB, 3," finally");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3," if (sqlCon.State == System.Data.ConnectionState.Open)");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3,"sqlCon.Close();");
            AppendSpaceLine(ref strB, 3,"}");
            AppendSpaceLine(ref strB, 3,"SqlTran.Dispose();");

            AppendSpaceLine(ref strB, 3," sqlCon.Dispose();");
            AppendSpaceLine(ref strB, 3,"}");
            AppendSpaceLine(ref strB, 3,"return rs;");
            AppendSpaceLine(ref strB, 3,"}");
        //---------------------
        #endregion

             #region 方法体Delete
            AppendSpaceLine(ref strB, 3,"/// <summary>");
            AppendSpaceLine(ref strB, 3,"/// 根据Id删除一条记录");
            AppendSpaceLine(ref strB, 3,"/// </summary>");
            AppendSpaceLine(ref strB, 3,"/// <Decription>此代码由CodeGenerate自动生成，作者：彭承龙</Decription>");
            AppendSpaceLine(ref strB, 3,"/// <param name=\""+tbname.Substring(0,1).ToLower()+tbname.Substring(1)+".Id\"></param>");
            AppendSpaceLine(ref strB, 3,"/// <returns></returns>");
            AppendSpaceLine(ref strB, 3, "public static Result Delete(int Id)");
            AppendSpaceLine(ref strB, 3, "{");
            AppendSpaceLine(ref strB, 3,"OleDbConnection sqlCon = new OleDbConnection(Util.OleDbHelper.ConnectionString);");

            AppendSpaceLine(ref strB, 3,"sqlCon.Open();");

            AppendSpaceLine(ref strB, 3,"OleDbTransaction SqlTran = sqlCon.BeginTransaction();");

            AppendSpaceLine(ref strB, 3,"Result rs = new Result();");
            AppendSpaceLine(ref strB, 3,"try");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3, " rs = DBAccess" + strNamespace + "." + tbname + "D.Delete(SqlTran, Id);");

            AppendSpaceLine(ref strB, 3," if (rs.IsSuccess)");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3," SqlTran.Commit();");
            AppendSpaceLine(ref strB, 3," }");
            AppendSpaceLine(ref strB, 3," else");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3," SqlTran.Rollback();");
            AppendSpaceLine(ref strB, 3,"}");
            AppendSpaceLine(ref strB, 3,"}");
            AppendSpaceLine(ref strB, 3,"catch (Exception ex)");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3," SqlTran.Rollback();");

            AppendSpaceLine(ref strB, 3," rs.Msg = ex.Message;");

            AppendSpaceLine(ref strB, 3,"throw ex;");
            AppendSpaceLine(ref strB, 3,"}");
            AppendSpaceLine(ref strB, 3,"finally");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3,"if (sqlCon.State == System.Data.ConnectionState.Open)");
            AppendSpaceLine(ref strB, 3,"{");
            AppendSpaceLine(ref strB, 3," sqlCon.Close();");
            AppendSpaceLine(ref strB, 3," }");
            AppendSpaceLine(ref strB, 3," SqlTran.Dispose();");
            AppendSpaceLine(ref strB, 3,"sqlCon.Dispose();");
            AppendSpaceLine(ref strB, 3,"}");
            AppendSpaceLine(ref strB, 3," return rs;");
            AppendSpaceLine(ref strB, 3," }");
             #endregion

             #region 获取Model
        AppendSpaceLine(ref strB, 3, "/// <summary>");
        AppendSpaceLine(ref strB, 3, "/// 根据Id获取一个"+tbname+"实体");
        AppendSpaceLine(ref strB, 3, "/// </summary>");
        AppendSpaceLine(ref strB, 3, "/// <Decription>此代码由CodeGenerate自动生成，作者：彭承龙</Decription>");
        AppendSpaceLine(ref strB, 3, "/// <param name=\"" + tbname+ ".Id\"></param>");
        AppendSpaceLine(ref strB, 3, "/// <returns></returns>");
        AppendSpaceLine(ref strB, 3, "public static Model" + strNamespace + "." + tbname + " GetModel(int id)");
        AppendSpaceLine(ref strB, 3,"{");
        AppendSpaceLine(ref strB, 3,"OleDbConnection sqlCon = new OleDbConnection(Util.OleDbHelper.ConnectionString);");
        AppendSpaceLine(ref strB, 3,"try");
        AppendSpaceLine(ref strB, 3,"{");
        AppendSpaceLine(ref strB, 3," sqlCon.Open();");
        AppendSpaceLine(ref strB, 3, "Model" + strNamespace + "." + tbname + " m_" + tbname + " = DBAccess" + strNamespace + "." + tbname + "D.GetModel(sqlCon, id);");
        AppendSpaceLine(ref strB, 3,"sqlCon.Close();");
        AppendSpaceLine(ref strB, 3, " return m_" + tbname + " ;");
        AppendSpaceLine(ref strB, 3," }");
        AppendSpaceLine(ref strB, 3," catch (Exception ex)");
        AppendSpaceLine(ref strB, 3,"{");
        AppendSpaceLine(ref strB, 3,"sqlCon.Close();");
        AppendSpaceLine(ref strB, 3,"throw ex;");
        AppendSpaceLine(ref strB, 3," }");
        AppendSpaceLine(ref strB, 3,"finally");
        AppendSpaceLine(ref strB, 3,"{");
        AppendSpaceLine(ref strB, 3," if (sqlCon.State == ConnectionState.Open) sqlCon.Close();");

        AppendSpaceLine(ref strB, 3,"sqlCon.Dispose();");
        AppendSpaceLine(ref strB, 3,"}");
        AppendSpaceLine(ref strB, 3,"}");
        #endregion

        #region 根据条件获取Models
        AppendSpaceLine(ref strB, 3,"/// <summary>");
        AppendSpaceLine(ref strB, 3," /// 通过条件返回对象"+tbname+"列表");
        AppendSpaceLine(ref strB, 3," /// </summary>");
        AppendSpaceLine(ref strB, 3, "/// <Decription>此代码由CodeGenerate自动生成，作者：彭承龙</Decription>");
        AppendSpaceLine(ref strB, 3,"/// <param name=\"strWhere\">查询条件</param>");
        AppendSpaceLine(ref strB, 3,"/// <returns>返回"+tbname+"数组</returns>");
        AppendSpaceLine(ref strB, 3, " public static Model" + strNamespace + "." + tbname + "[] GetModels(string strWhere)");
        AppendSpaceLine(ref strB, 3,"{");
        AppendSpaceLine(ref strB, 3, "Model" + strNamespace + "." + tbname + " [] m_"+tbname+"s = null;");
        AppendSpaceLine(ref strB, 3," Result rs = new Result();");
        AppendSpaceLine(ref strB, 3,"OleDbConnection conn = new OleDbConnection(Util.OleDbHelper.ConnectionString);");
        AppendSpaceLine(ref strB, 3,"conn.Open();");
        AppendSpaceLine(ref strB, 3,"try");
        AppendSpaceLine(ref strB, 3,"{");
        AppendSpaceLine(ref strB, 3, " m_" + tbname + "s = DBAccess" + strNamespace + "." + tbname + "D.GetModels(conn, strWhere);");
        AppendSpaceLine(ref strB, 3," rs.IsSuccess = true;");
        AppendSpaceLine(ref strB, 3," }");
        AppendSpaceLine(ref strB, 3,"catch (Exception e)");
        AppendSpaceLine(ref strB, 3,"{");
        AppendSpaceLine(ref strB, 3," rs.Msg = e.Message;");
        AppendSpaceLine(ref strB, 3," rs.IsSuccess = false;");
        AppendSpaceLine(ref strB, 3," throw e;");
        AppendSpaceLine(ref strB, 3," }");
        AppendSpaceLine(ref strB, 3," finally");
        AppendSpaceLine(ref strB, 3," {");
        AppendSpaceLine(ref strB, 3,"  if (conn.State == System.Data.ConnectionState.Open)");
        AppendSpaceLine(ref strB, 3,"{");
        AppendSpaceLine(ref strB, 3,"conn.Close();");
        AppendSpaceLine(ref strB, 3," }");
        AppendSpaceLine(ref strB, 3,"conn.Dispose();");
        AppendSpaceLine(ref strB, 3," }");
        AppendSpaceLine(ref strB, 3, " return m_" + tbname + "s;");
        AppendSpaceLine(ref strB, 3,"}");
        #endregion

        //类 命名空间结束
            AppendSpaceLine(ref strB, 2, "}");
            AppendSpaceLine(ref strB, 0, "}");
            string strdir = "F:\\code\\Bussiness\\" + strNamespace.Replace(".", "") + "\\";
            if (!Directory.Exists(strdir))
            {
                Directory.CreateDirectory(strdir);
            }
            SaveStringToFile(strB.ToString(), strdir + tbname + "B.cs");
            return strMsg;
        }

       public static void BuildDBAccess(string id, string tbname, string strNamespace)
        {
            //操作类
            string strnamespace = strNamespace;

            if (strnamespace != "")
            {
                strnamespace = "." + strnamespace;
            }  

            DataTable dt = OleDBCommon.OleDBObject.GetColumnList(tbname);
            tbname = tbname.ToUpper().Substring(0, 1) + tbname.Substring(1);
            string str = "2222using System;\nusing System.Data;\nusing System.Data.OleDb;\r\nusing Model" + strnamespace + ";\r\nusing DBAccess;\r\nusing Util;\r\n";
            str = str + " namespace DBAccess" + strnamespace + " \r\n{ [Serializable]\r\npublic class " + tbname + "D\n{";

            string markadd = " \n/// <summary>\n" +
                           " /// 添加表" + tbname + "记录并返回ID\n" +
                           " /// </summary>\n" +
                           " /// <param name=\"tb\">表" + tbname + "对象</param>\n" +
                           " /// <returns>返回ID</returns>\n";
            string markupdate = " \n /// <summary>\n" +
                                 " /// 修改表" + tbname + "记录并反回影响行数\n" +
                                 " /// </summary>\n" +
                                 " /// <param name=\"tb\">表" + tbname + "对象</param>\n" +
                                 " /// <returns>返回影响行数</returns>\n";
            string markdel = "  \n/// <summary>\n" +
                             " /// 通过ID删除记录\n" +
                             " /// </summary>\n" +
                             " /// <param name=\"id\">ID号</param>\n" +
                             " /// <returns>返回影响行数</returns>\n";

            string markdelWhere = "  \n/// <summary>\n" +
                            " /// 按条件删除记录\n" +
                            " /// </summary>\n" +
                            " /// <param name=\"strWhere\">删除的条件如：name='Jim'</param>\n" +
                            " /// <returns>返回影响行数</returns>\n";

            string markgettb = "  \n/// <summary>\n" +
                             " /// 通过ID返回对象" + tbname + "\n" +
                             " /// </summary>\n" +
                             " /// <param name=\"id\">ID号</param>\n" +
                             " /// <returns>返回" + tbname + "</returns>\n";
            string markgetdatareader = "  \n/// <summary>\n" +
                                       " /// 分页返回" + tbname + " datareader\n" +
                                       " /// </summary>\n" +
                                       " ///  <param name=\"pagesize\">返回记录数量</param>\n" +
                                       " ///  <param name=\"index\">当前第几页</param>\n" +
                                       " /// <returns>返回" + tbname + " 记录集</returns>\n";

            string add = markadd + "public  static Result  Add(OleDbTransaction OleDbTrans,Model" + strnamespace + "." + tbname + " tb)\n{\r\n ";
            string strSql = "string strSql=\" insert into [" + tbname + "](";
            string addField = "";
            string addPara = "";

            string updateNameID = "";

            string update = markupdate + "public  static Result Update (OleDbTransaction OleDbTrans ,Model" + strnamespace + "." + tbname + " tb)\n{\n  \n";
            string strUpdate = "string strSql =\"update [" + tbname + "] set ";
            string updateField = "";

            string gettb = markgettb + "public  static Model" + strnamespace + "." + tbname + "  GetModel( OleDbTransaction  OleDbTrans ,int id)\n{\n  Model" + strnamespace + "." + tbname + "  tb=null; \r\n";
            gettb = gettb + "  try{  DataSet ds = new DataSet();\r\n ds =  OleDbHelper.ExecuteDataset(OleDbTrans, CommandType.Text, \"select * from [" + tbname + "] where id=\"+id.ToString()); \n";
            gettb = gettb + " DataView dv = ds.Tables[0].DefaultView;\r\n                if (dv.Count > 0)      \r\n         {\r\n";
            gettb = gettb + " tb=new Model" + strnamespace + "." + tbname + "(); \n";

            string getModel = markgettb + "public  static Model" + strnamespace + "." + tbname + "  GetModel(OleDbConnection OleDbconn,int id)\n{\n  Model" + strnamespace + "." + tbname + "  tb=null;OleDbDataReader dr=null ;  \r\n";
            getModel = getModel + "  try{ dr =  OleDbHelper.ExecuteReader(OleDbconn, CommandType.Text, \"select * from [" + tbname + "] where id=\"+id.ToString()); \n";
            getModel = getModel + " if(dr.Read())\n{ tb=new Model" + strnamespace + "." + tbname + "(); \n";

            string getModelsOleDbconn = "  \n/// <summary>\n" +
                             " /// 通过条件返回对象" + tbname + "列表\n" +
                             " /// </summary>\n" +
                             " /// <param name=\"strWhere\">条件如name='aaa',小心注入攻击</param>\n" +
                             " /// <returns>返回" + tbname + "</returns>\r\n" +
                             "public static Model" + strnamespace + "." + tbname + "[] GetModels(OleDbConnection OleDbconn,string strWhere){\r\n" +
                             "try\r\n{\r\n" +
                              "string strSql = \"\"; if (strWhere == \"\") { strWhere = \"  1=1\"; }\r\n " +
                                 " strSql = \"select * from [" + tbname + "] Where \" + strWhere;\r\n " +
                                 "DataSet ds = OleDbHelper.ExecuteDataset(OleDbconn, CommandType.Text, strSql);    \r\n        DataView dv =ds.Tables[0].DefaultView;\r\n" +

                                 " int total = dv.Count;\r\n Model" + strnamespace + "." + tbname + "[] tbs = new Model" + strnamespace + "." + tbname + "[total];\r\n" +
                                 " for (int i = 0; i < dv.Count; i++) {  tbs[i] = new Model" + strnamespace + "." + tbname + "();";



            string getModelsOleDbTrans = "  \n/// <summary>\n" +
                         " /// 通过条件返回对象" + tbname + "列表\n" +
                         " /// </summary>\n" +
                         " /// <param name=\"strWhere\">条件如name='aaa',小心注入攻击</param>\n" +
                         " /// <returns>返回" + tbname + "</returns>\r\n" +
                         "public static Model" + strnamespace + "." + tbname + "[] GetModels(OleDbTransaction  OleDbTrans ,string strWhere){\r\n" +
                         "try\r\n{\r\n" +
                          "string strSql = \"\"; if (strWhere == \"\") { strWhere = \"  1=1\"; }\r\n " +
                             " strSql = \"select * from [" + tbname + "] Where \" + strWhere;\r\n " +
                             "DataSet ds = OleDbHelper.ExecuteDataset(OleDbTrans, CommandType.Text, strSql);    \r\n        DataView dv =ds.Tables[0].DefaultView;\r\n" +

                             " int total = dv.Count;\r\n Model" + strnamespace + "." + tbname + "[] tbs = new Model" + strnamespace + "." + tbname + "[total];\r\n" +
                             " for (int i = 0; i < dv.Count; i++) {  tbs[i] = new Model" + strnamespace + "." + tbname + "();";

            string getModels = "";



            string para = "   OleDbParameter[] parameters ={ \n ";
            string vals = "";
            int k = 0;
            //string strClass = "public class TB" + tbname + "\n{   \n public int ID;  \n";
            string strClass = "  \n  ";
            bool HasRecord = false;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                HasRecord = true;
                string name = Convert.ToString(dt.Rows[i]["COLUMN_NAME"]).Trim();
                name = name.ToUpper().Substring(0, 1) + name.Substring(1);
                string type = Convert.ToString(dt.Rows[i]["typename"]).Trim().ToLower();
                string size = Convert.ToString(dt.Rows[i]["CHARACTER_MAXIMUM_LENGTH"]);

                if (name.ToLower() != "id")
                {
                    addField += "[" + name + "],";
                    addPara += "@" + name + ",";
                    updateField += name + "=@" + name + ",";
                }

                if (name.ToLower() == "id" && type == "int")
                {
                    getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dr[\"" + name + "\"]);} \n";
                    gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dv[0][\"" + name + "\"]);} \n";
                    getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt32(dv[i][\"" + name + "\"]);}\r\n";
                }
                else if (type == "int")
                {
                    getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dr[\"" + name + "\"]);} \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Integer),\n";
                    gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dv[0][\"" + name + "\"]);} \n";
                    getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt32(dv[i][\"" + name + "\"]);}\r\n";

                }
                else if (type.ToLower() == "short")
                {
                    getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt16(dr[\"" + name + "\"]);} \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Integer),\n";
                    gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt16(dv[0][\"" + name + "\"]);} \n";
                    getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt16(dv[i][\"" + name + "\"]);}\r\n";

                }
                else if (type == "bool")
                {
                    getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToBoolean(dr[\"" + name + "\"]);} \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Boolean),\n";
                    gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToBoolean(dv[0][\"" + name + "\"]);} \n";
                    getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToBoolean(dv[i][\"" + name + "\"]);}\r\n";
                }
                else if (type == "string")
                {
                    getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.VarChar," + size + "),\n";
                    gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                    getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
                }

                else if (type == "char")
                {
                    getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Char," + size + "),\n";
                    gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                    getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
                }
                else if (type == "nchar")
                {
                    getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.NChar," + size + "),\n";
                    gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                    getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
                }
                else if (type == "nvarchar")
                {
                    getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.NVarChar," + size + "),\n";
                    gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                    getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
                }
                else if (type == "float" || type == "double")
                {
                    getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dr[\"" + name + "\"]);} \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Double),\n";
                    gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dv[0][\"" + name + "\"]);} \n";
                    getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDouble(dv[i][\"" + name + "\"]);}\r\n";
                }
                else if (type == "decimal")
                {
                    getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dr[\"" + name + "\"]);} \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Double),\n";
                    gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dv[0][\"" + name + "\"]);} \n";
                    getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDouble(dv[i][\"" + name + "\"]);}\r\n";
                }
                else if (type == "numeric")
                {
                    getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dr[\"" + name + "\"]);} \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Decimal),\n";
                    gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dv[0][\"" + name + "\"]);} \n";
                    getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDouble(dv[i][\"" + name + "\"]);}\r\n";
                }
                else if (type == "datetime")
                {
                    getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dr[\"" + name + "\"]);} \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Date),\n";
                    gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dv[0][\"" + name + "\"]);} \n";

                    getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDateTime(dv[i][\"" + name + "\"]);}\r\n";

                }
                else if (type.ToLower() == "smalldatetime")
                {
                    getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dr[\"" + name + "\"]);} \n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.SmallDateTime),\n";
                    gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dv[0][\"" + name + "\"]);} \n";
                    getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDateTime(dv[i][\"" + name + "\"]);}\r\n";

                }
                else if (type == "image")
                {
                    getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =(Image ) dr[\"" + name + "\"];} \n";
                    //   para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Binary),\n";
                    para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Image),\n";
                    gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " = (Image ) dv[0][\"" + name + "\"] ;} \n";
                    getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " =  (Image ) dv[i][\"" + name + "\"] ;}\r\n";

                }
                else
                {
                    CommonClass.Alert("<br>Error:" + id + "-" + tbname + "-" + type);

                }
                //OleDbType.Float 
                if (name.ToLower() != "id")
                {
                    vals = vals + "parameters [" + k.ToString() + "].Value=Util.PublicClass.GetNullValue(tb." + name + ");\n";
                    k++;
                }
                else
                {
                    updateNameID = name;
                }
            }




            if (!HasRecord)
            {
                CommonClass.Alert("该表没有任何字段");

            }
            /*添加数据方法定义*/
            addField = addField.Substring(0, addField.Length - 1);
            addPara = addPara.Substring(0, addPara.Length - 1);

            strSql = strSql + addField + ")  \"\r\n+\" values(" + addPara + ")\"\r\n;";

            add = add + strSql +";4444444"+
                "\r\n Result rs = new Result();\r\n" +
                "try\r\n{\r\n" + para.Substring(0, para.Length - 2) + " \r\n   };\n" +
                vals + "\n " +
                "rs.DataResult = OleDbHelper.ExecuteDataset(OleDbTrans, CommandType.Text, strSql, parameters);\r\n" +
                                "rs.DataResult = OleDbHelper.ExecuteDataset(OleDbTrans, CommandType.Text, \" select max(id) from "+tbname+"\", null);\r\n" +
                "if (rs.DataResult.Tables.Count > 0 && rs.DataResult.Tables[0].Rows.Count > 0 && !Convert.IsDBNull(rs.DataResult.Tables[0].Rows[0][0]))\r\n { \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
                    "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
                    "\r\nfinally{}" +
                    "\r\nreturn rs;\r\n}\r\n";

            /*修改数据方法定义区*/
            updateField = updateField.Substring(0, updateField.Length - 1);
            strUpdate = strUpdate + updateField + " where id=@id\";";
            update = update + strUpdate + "\r\n   Result rs = new Result();\r\n try\r\n{\r\n" +
                para + "   new OleDbParameter(\"@ID\",OleDbType.Integer)   };\n" +
                vals + " \r\nparameters[" + k.ToString() + "].Value=tb." + updateNameID + ";\r\n" +
               " int iflag = OleDbHelper.ExecuteNonQuery(OleDbTrans, CommandType.Text, strSql, parameters);\r\n " +
                "if (iflag>0) { rs.IsSuccess = true; } else { rs.IsSuccess = false; } }\r\n" +
                " catch (Exception ex) { \r\nrs.IsSuccess = false; \r\nrs.Msg = ex.Message; \r\nthrow ex; \r\n}\r\n" +
                " finally { }\r\n" +
                " return rs;\r\n}\r\n";


            /*按ID删除*/
            string delete = markdel + "public static Result  Delete (OleDbTransaction OleDbTrans,int id)\n{ \n " +
                "string strSql=\"delete from [" + tbname + "] where id=\"+id.ToString();\r\n" +
                " Result rs = new Result();\r\n try\r\n{\r\n" +
                " int iflag = OleDbHelper.ExecuteNonQuery(OleDbTrans, CommandType.Text, strSql, null);\r\n " +
                "  rs.IsSuccess = true;   }\r\n" +
                " catch (Exception ex) { \r\nrs.IsSuccess = false; \r\nrs.Msg = ex.Message; \r\nthrow ex; \r\n}\r\n" +
                " finally { }\r\n" +
                " return rs;\r\n}\r\n";

            /*按条件删除*/
            markdelWhere = markdelWhere + "\r\npublic static Result  Delete (OleDbTransaction OleDbTrans,string strWhere)\n{ \n " +
                "string strSql=\"\";\r\n" +
                 " Result rs = new Result();\r\n try\r\n{\r\n" +
                "if (strWhere.Trim() != \"\") { strSql = \"delete from [" + tbname + "]  where \" + strWhere; } else { rs.IsSuccess = false; rs.Msg = \"未传入删除条件\"; return rs; }" +
                " int iflag = OleDbHelper.ExecuteNonQuery(OleDbTrans, CommandType.Text, strSql, null);\r\n " +
                "  rs.IsSuccess = true;   }\r\n" +
                " catch (Exception ex) { \r\nrs.IsSuccess = false; \r\nrs.Msg = ex.Message; \r\nthrow ex; \r\n}\r\n" +
                " finally { }\r\n" +
                " return rs;\r\n}\r\n";


            //按ID查得某条记录
            gettb = gettb + "  \r\n}\r\n}catch (Exception ex){throw ex;}\r\nfinally{ }\r\nreturn tb;}\r\n";
            getModel = getModel + "  \r\n}\r\n}catch (Exception ex){throw ex;}\r\nfinally{dr.Close();dr.Dispose();}\r\nreturn tb;}\r\n";
            //返回model 列表

            getModelsOleDbTrans = getModelsOleDbTrans + getModels + "}\r\n return tbs;}catch (Exception ex){ throw ex;} }";

            getModelsOleDbconn = getModelsOleDbconn + getModels + "}\r\n return tbs;}catch (Exception ex){ throw ex;} }";



            //分页获取数据
            string strpage = markgetdatareader + "public static  Result  GetDataSet(OleDbConnection OleDbconn,int pagesize,int index)\n";
            strpage += "{\n";
            strpage += "int nextpage=pagesize*(index-1);\n" +
            " string strSql = \"select top \" + pagesize.ToString() + \" * from   [" + tbname.Replace("'", "").Trim() + "]  where id not in (select top \" + nextpage.ToString() + \" id from  [" + tbname.Replace("'", "").Trim() + "]  order by id desc) order by id desc\";\n" +
           " Result rs = new Result();\r\n try\r\n{\r\n" +
            " rs.DataResult = OleDbHelper.ExecuteDataset(OleDbconn, CommandType.Text, strSql); \r\n" +
            "if (rs.DataResult.Tables.Count > 0)" +
            "{ \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
            "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
            "\r\nfinally{}" +
            "\r\nreturn rs;\r\n}\r\n";



            //获取全部数据
            string strGetAllData = "public static  Result  GetDataSet(OleDbConnection OleDbconn)\n";
            strGetAllData += "{\n";
            strGetAllData += " string strSql = \"select   * from   [" + tbname.Replace("'", "").Trim() + "]  order by id  \";\n" +
           " Result rs = new Result();\r\n try\r\n{\r\n" +
            " rs.DataResult = OleDbHelper.ExecuteDataset(OleDbconn, CommandType.Text, strSql); \r\n" +
            "if (rs.DataResult.Tables.Count > 0)" +
            "{ \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
            "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
            "\r\nfinally{ }" +
            "\r\nreturn rs;\r\n}\r\n";

            //统计共有多少条数据
            string strCount = "public static Result Count(OleDbConnection OleDbConn)\n";
            strCount += "{\n" +
            " string strSql = \"select count(*)   from   [" + tbname.Replace("'", "").Trim() + "]\";\n" +
            " Result rs = new Result();\r\n try\r\n{\r\n" +
            " rs.DataResult = OleDbHelper.ExecuteDataset(OleDbConn, CommandType.Text, strSql); \r\n" +
            "if (rs.DataResult.Tables.Count > 0 && rs.DataResult.Tables[0].Rows.Count > 0)" +
            "{ \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
            "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
            "\r\nfinally{ }" +
            "\r\nreturn rs;\r\n}\r\n";

            //string connstr = "\r\n public string connstr = Util.PublicClass.GetConnStr();";
            str = str + "\n" + add + "\n" + update + delete + markdelWhere + gettb + getModel + getModelsOleDbconn + getModelsOleDbTrans + strpage + strGetAllData + strCount + "\n}\r\n}";
            string strdir = "F:\\code\\DBAccess\\" + strnamespace.Replace(".", "") + "\\";
            if (!Directory.Exists(strdir))
            {
                Directory.CreateDirectory(strdir);
            }
            SaveStringToFile(str, strdir + tbname + "D.cs");

        }

        static void BuildModel(string strNamespace, string tbname)
       {
           string strnamespace = strNamespace;
           if (strnamespace != "")
           {
               strnamespace = "." + strnamespace;
           }
           DataTable dt = OleDBCommon.OleDBObject.GetColumnList(tbname);
           tbname = tbname.ToUpper().Substring(0, 1) + tbname.Substring(1);
           string str = "using System;\n\n";
           str = str + "namespace Model" + strnamespace + "\r\n{\r\n[Serializable]\r\npublic class " + tbname + " \r\n{";
           int k = 0;
           // string strClass = "  \n private int _ID; public int ID\r\n{\r\nget{return _ID;}\r\nset{_ID=value;}\r\n} \n";
           string strClass = "";
           bool HasRecord = false;
           for (int i = 0; i < dt.Rows.Count; i++)
           {
               HasRecord = true;
               string name = Convert.ToString(dt.Rows[i]["COLUMN_NAME"]).Trim();
               name = name.ToUpper().Substring(0, 1) + name.Substring(1);
               string type = dt.Rows[i]["TypeName"].ToString();// GetType(Convert.ToString(dt.Rows[i]["xtype"])).ToLower();
               string size = dt.Rows[i]["CHARACTER_MAXIMUM_LENGTH"].ToString();
               string desc = "\r\n  /// <summary>\r\n" +
                            "   /// " + dt.Rows[i]["DESCRIPTION"].ToString() + "\r\n" +
                            "   /// </summary>\r\n";

               if (type == "int" || type == "short")
               {
                   strClass = strClass + " private int _" + name + ";\r\n";
                   strClass = strClass + desc;
                   strClass = strClass + " public int " + name + "\r\n";
                   strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                   strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
               }
               else if (type == "bool")
               {
                   strClass = strClass + " private bool _" + name + ";\r\n";
                   strClass = strClass + desc;
                   strClass = strClass + " public bool " + name + "\r\n";
                   strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                   strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
               }
               else if (type == "byte")
               {
                   strClass = strClass + " private byte[] _" + name + ";\r\n";
                   strClass = strClass + desc;
                   strClass = strClass + " public byte[] " + name + "\r\n";
                   strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                   strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
               }
               else if (type == "string")
               {

                   //strClass = strClass +desc;strClass = strClass + " public string  " + name + ";\n";

                   strClass = strClass + " private string _" + name + ";\r\n";
                   strClass = strClass + desc;
                   strClass = strClass + " public string " + name + "\r\n";
                   strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                   strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
               }

               else if (type == "datetime")
               {

                   // strClass = strClass +desc;strClass = strClass + " public DateTime " + name + ";\n";

                   strClass = strClass + " private DateTime _" + name + ";\r\n";
                   strClass = strClass + desc;
                   strClass = strClass + " public DateTime " + name + "\r\n";
                   strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                   strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
               }
               else if (type == "single" || type == "double")
               {

                   // strClass = strClass +desc;strClass = strClass + " public double " + name + ";\n";

                   strClass = strClass + " private double _" + name + ";\r\n";
                   strClass = strClass + desc;
                   strClass = strClass + " public double " + name + "\r\n";
                   strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                   strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
               }
               else if (type == "littlenum")
               {

                   // strClass = strClass +desc;strClass = strClass + " public double " + name + ";\n";

                   strClass = strClass + " private double _" + name + ";\r\n";
                   strClass = strClass + desc;
                   strClass = strClass + " public double " + name + "\r\n";
                   strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                   strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
               }
               else if (type == "image")
               {
                   strClass = strClass + " private Image _" + name + ";\r\n";
                   strClass = strClass + desc;
                   strClass = strClass + " public Image " + name + "\r\n";
                   strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                   strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
               }
               else if (type == "bit")
               {
                   strClass = strClass + " private bool _" + name + ";\r\n";
                   strClass = strClass + desc;
                   strClass = strClass + " public bool " + name + "\r\n";
                   strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                   strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
               }
               else
               {
                   CommonClass.Alert("<br>Error:" + type + "--- 表" + tbname);

               }
               strClass = strClass + "\r\n";            //SqlDbType.Float 


           }

           if (!HasRecord)
           {
               CommonClass.Alert("<br>Error:该表没有任何字段- -表" + tbname);

           }
           string strdir = "F:\\code\\model\\" + strnamespace.Replace(".", "") + "\\";
           if (!Directory.Exists(strdir))
           {
               Directory.CreateDirectory(strdir);
           }
           str = str + "\r\n" + strClass + "\n" + "\n}\r\n}\r\n";
           SaveStringToFile(str, strdir + tbname + ".cs");
           //  SaveStringToFile(str, Server.MapPath("app_code/" + tbname + ".txt"));
           // Response.Write(tbname + "<br>");
       }
     
       static void SaveStringToFile(string str, string filepath)
        {
            StreamWriter stw = new StreamWriter(filepath, false, System.Text.Encoding.Default);
            stw.Write(str);
            stw.Close();
        }
       /// <summary>
       /// 增加Tab空格缩进或间隔
       /// </summary>
       /// <param name="num">间隔数</param>
       /// <returns></returns>
        static string Space(int SpaceNum)
       {
           StringBuilder strspace = new StringBuilder();
           for (int n = 0; n < SpaceNum; n++)
           {
               strspace.Append("\t");
           }
           return strspace.ToString();
       }
       /// <summary>
       /// 追加一行文本，前面加空格缩进，后面带回车换行。
       /// </summary>
       /// <param name="SpaceNum">空格缩进数目</param>
       /// <param name="Text">文本</param>
       /// <returns></returns>
        static void AppendSpaceLine(ref StringBuilder str, int SpaceNum, string Text)
       {         
           str.Append(Space(SpaceNum));
           str.Append(Text);
           str.Append("\r\n");
       }
       /// <summary>
       /// 追加回车换行。
       /// </summary>		
        static void AppendLine(ref StringBuilder str)
       {
           str.Append("\r\n");        
       }
       /// <summary>
       /// 追加一行文本，带回车换行。
       /// </summary>
       /// <param name="Text">文本</param>
       /// <returns></returns>
        static void AppendLine(ref StringBuilder str, string Text)
       {
           str.Append(Text + "\r\n");         
       }

    }
}